package com.example.demo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Student;
import com.example.demo.entity.StudentAccount;
import com.example.demo.mapper.StudentAccountMapper;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.service.StudentService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public class StudentImpl implements StudentService {
    private final StudentMapper studentMapper;
    private final StudentAccountMapper studentAccountMapper;

    @Override
    public Collection<Student> getAllStudent() {
        List<Student> allStudent = studentMapper.selectList(null);
        return allStudent;
    }

    @Override
    public List<Student> filterStudent(Student student) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if (student.getStudentName() != null && !student.getStudentName().isEmpty()) {
            queryWrapper.like("student_name", student.getStudentName());
        }
        if (student.getStudentId() != null) {
            queryWrapper.like("student_id", student.getStudentId());
        }
        if (student.getStudentIdentityId() != null && !student.getStudentIdentityId().isEmpty()) {
            queryWrapper.like("student_identity_id", student.getStudentIdentityId());
        }
        if (student.getStudentSex() != null && !student.getStudentSex().isEmpty()) {
            queryWrapper.like("student_sex", student.getStudentSex());
        }
//        queryWrapper.like("student_name",student.getStudentName())
//                .like("student_id",student.getStudentId())
//                .like("student_identity_id",student.getStudentIdentityId())
//                .like("student_sex",student.getStudentSex());
        List<Student> filterList = studentMapper.selectList(queryWrapper);
        return filterList;
    }

    @Override
    public Student addStudent(Student student) {
//        String uniqueId = UUID.randomUUID().toString();
//        student.setStudentId(uniqueId);
        int result = studentMapper.insert(student);
        if (result > 0) {
            return student;
        } else {
            throw new RuntimeException("Failed to add student");
        }
    }

    @Override
    public Student addStudent(Student student,Long studentAccountId) {
//        String uniqueId = UUID.randomUUID().toString();
//        student.setStudentId(uniqueId);
        int result = studentMapper.insert(student);
        Long id=student.getStudentId();
        UpdateWrapper<StudentAccount> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("student_account_id",studentAccountId).set("student_id",id);
        studentAccountMapper.update(updateWrapper);
        if (result > 0) {
            return student;
        } else {
            throw new RuntimeException("Failed to add student");
        }
    }

    @Override
    public Student updateStudent(Student student) {
        int result = studentMapper.updateById(student);
        if(result>0){
            return student;
        } else {
            throw new RuntimeException("Failed to update student");
        }
    }

    @Override
    public Long deleteStudent(Long studentId) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id", studentId);
        int result = studentMapper.delete(queryWrapper);
        if (result > 0) {
            return studentId;
        } else {
            throw new RuntimeException("Failed to delete student");
        }
    }

    @Override
    public Student getStudentByStudentId(Long studentId) {
        return studentMapper.findById(studentId);
    }

    @Override
    public StudentAccount getStudentAccountByStudentId(Long studentId) {
        return studentAccountMapper.findByStudentId(studentId);
    }

    @Override
    public StudentAccount updateSelfAccount(StudentAccount studentAccount) {
        int result = studentAccountMapper.updateById(studentAccount);
        if(result>0){
            return studentAccount;
        }
        else {
            throw new RuntimeException("Failed to update studentAccount");
        }
    }

    @Override
    public IPage pageFindStudent(int page, int piece) {
        Map<String,Object> map = new HashMap<>();
        Page page1 = new Page(page,piece);
        IPage iPage = studentMapper.selectPage(page1,null);
        System.out.println(iPage.getRecords().size());

        return iPage;
    }
    @Override
    public StudentAccount updatePassword(String account, String oldPassword, String newPassword){
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("student_account_id",account);
        updateWrapper.eq("student_password",oldPassword);
        updateWrapper.set("student_password",newPassword);
        int result = studentAccountMapper.update(updateWrapper);
        if(result>0){
            return studentAccountMapper.findById(Long.valueOf(account));
        }
        else {
            throw new RuntimeException("Failed to update studentAccount");
        }
    }
}
